<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>欢迎登录</h2>
        <p>甄淘电商，品质生活从这里开始</p>
      </div>
      
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="ruleForm.username"
            prefix-icon="User"
            placeholder="请输入账号"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input 
            v-model="ruleForm.password"
            prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
            show-password
          />
        </el-form-item>
        
        <div class="form-footer">
          <el-button 
            type="primary" 
            class="login-button"
            :loading="loading"
            @click="submitForm(ruleFormRef)"
          >
            登录
          </el-button>
          
          <el-button 
            text
            class="reset-button"
            @click="resetForm(ruleFormRef)"
          >
            重置
          </el-button>
        </div>
      </el-form>
      
      <div class="login-footer">
        <p>还没有账号？<el-button @click="zc">立即注册</el-button></p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { login } from "@/assets/login.js"
import { ElMessage } from "element-plus"
import router from "@/router/index.js"
import { User, Lock } from '@element-plus/icons-vue'

const ruleFormRef = ref()
const loading = ref(false)

const ruleForm = ref({
  username: "",
  password: ""
})

const rules = ref({
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
})

function submitForm() {
  loading.value = true
  login(ruleForm.value).then(res => {
    localStorage.setItem("userId", res.data.id)
    localStorage.setItem("username",res.data.username)
    ElMessage.success("登录成功")
    router.push("/about")
  }).catch(err => {
    ElMessage.error("登录失败，请检查账号密码")
  }).finally(() => {
    loading.value = false
  })
}
function zc(){
  router.push("/zhuce")
}
const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1890ff 0%, #36cfc9 100%);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
  animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h2 {
  font-size: 24px;
  color: #303133;
  margin: 0 0 10px;
}

.login-header p {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.login-form {
  margin-bottom: 20px;
}

.login-form :deep(.el-input) {
  height: 40px;
}

.login-form :deep(.el-input__wrapper) {
  background-color: #f5f7fa;
  border: none;
  transition: all 0.3s ease;
}

.login-form :deep(.el-input__wrapper:hover) {
  background-color: #e6e8eb;
}

.login-form :deep(.el-input__wrapper.is-focus) {
  background-color: white;
  box-shadow: 0 0 0 1px #409EFF;
}

.form-footer {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 30px;
}

.login-button {
  width: 100%;
  height: 40px;
  font-size: 16px;
}

.reset-button {
  font-size: 14px;
}

.login-footer {
  text-align: center;
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #ebeef5;
}

.login-footer p {
  color: #606266;
  font-size: 14px;
  margin: 0;
}

@media (max-width: 480px) {
  .login-box {
    margin: 20px;
    padding: 30px;
  }
}
</style>
